package com.bio.action.information;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.bio.action.AbstractAction;
import com.bio.entity.basicinfo.Basicinfo;
import com.bio.entity.information.Information;
import com.bio.service.basicinfo.BasicinfoService;
import com.bio.service.information.InformationService;
import com.bio.utils.Environment;

@InterceptorRefs(value = { @InterceptorRef(value="fileUpload") ,@InterceptorRef(value="defaultStack")})
@Namespace("/information")
@Results( {
		@Result(name = "list", location = "/WEB-INF/views/information/listInformations.jsp", type = "dispatcher"),
		@Result(name = "listDetail", location = "/WEB-INF/views/information/listDetail.jsp", type = "dispatcher"),
		@Result(name = "tolist", location = "information!list", type = "redirect"),
		@Result(name = "detail", location = "/WEB-INF/views/information/listDetail.jsp", type = "dispatcher"),
		@Result(name = "edit", location = "/WEB-INF/views/information/editInformation.jsp", type = "dispatcher"),
		@Result(name = "add", location = "/WEB-INF/views/information/addInformation.jsp", type = "dispatcher")
		})
public class InformationAction extends AbstractAction<Information, InformationService> implements Serializable{

	private Information information;
	
	private List<Basicinfo> basicInfos;
	
	private BasicinfoService basicinfoService;
	
	public InformationAction(InformationService service) {
		super(service);
	}

	private static final long serialVersionUID = 336165974133512495L;

	public String listDetail(){
		information = service.get(id);
		return "detail";
	}
	public String list(){
		Long companyId = Environment.Session.getCompanyId();
		pagination.setCurrentIndex(getCurrentIndex());
		pager = service.findPagerByCompanyId(pagination, companyId);
		return "list";
	}
	
	public String add(){
		Long companyId = Environment.Session.getCompanyId();
		pagination.setCurrentIndex(getCurrentIndex());
		basicInfos = basicinfoService.findPagerByType(pagination, Integer.valueOf(3), companyId).getResult();
		service.findAllByPager(pagination);
		return "add";
	}
	
	public String save(){
		service.save(information);
		return "tolist";
	}
	
	public String edit(){
		Long companyId = Environment.Session.getCompanyId();
		basicInfos = basicinfoService.findPagerByType(pagination, Integer.valueOf(3), companyId).getResult();
		information = service.get(id);
		return "edit";
	}
	
	public String update(){
		Information old = this.service.get(information.getId());
		old.setTitle(information.getTitle());
		old.setTypeId(information.getTypeId());
		old.setTypeName(information.getTypeName());
		old.setUpdatedDate(Calendar.getInstance());
		old.setContent(information.getContent());
		old.setAttachments(information.getAttachments());
		old.setStatus(information.getStatus());
		old.setToDeletePics(information.getToDeletePics());
		//TODO
		service.save(old);
		return "tolist";
	}

	public Information getInformation() {
		return information;
	}

	public void setInformation(Information information) {
		this.information = information;
	}

	public void setBasicinfoService(BasicinfoService basicinfoService) {
		this.basicinfoService = basicinfoService;
	}

	public List<Basicinfo> getBasicInfos() {
		return basicInfos;
	}

	public void setBasicInfos(List<Basicinfo> basicInfos) {
		this.basicInfos = basicInfos;
	}
}
